package com.nehc.nettyserver.controller;

import com.nehc.nettyserver.common.model.Message;
import com.nehc.nettyserver.service.NotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通知控制器
 * 提供通知相关API
 * 
 * @author NEHC
 */
@RestController
@RequestMapping("/notifications")
@RequiredArgsConstructor
public class NotificationController {
    
    private final NotificationService notificationService;
    
    /**
     * 发送系统通知给所有用户
     */
    @PostMapping("/broadcast")
    public ResponseEntity<Map<String, Object>> broadcastNotification(@RequestBody Map<String, Object> payload) {
        Map<String, Object> result = new HashMap<>();
        
        String title = (String) payload.getOrDefault("title", "系统通知");
        String content = (String) payload.get("content");
        
        if (content == null || content.trim().isEmpty()) {
            result.put("success", false);
            result.put("message", "通知内容不能为空");
            return ResponseEntity.badRequest().body(result);
        }
        
        // 移除标题和内容，剩余的作为附加参数
        Map<String, Object> params = new HashMap<>(payload);
        params.remove("title");
        params.remove("content");
        
        int recipientCount = notificationService.sendSystemNotification(title, content, params);
        
        result.put("success", recipientCount > 0);
        result.put("recipientCount", recipientCount);
        result.put("message", recipientCount > 0 ? "通知发送成功" : "通知发送失败，没有在线用户");
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 发送系统通知给指定用户
     */
    @PostMapping("/user/{userId}")
    public ResponseEntity<Map<String, Object>> sendNotificationToUser(
            @PathVariable String userId,
            @RequestBody Map<String, Object> payload) {
        
        Map<String, Object> result = new HashMap<>();
        
        String title = (String) payload.getOrDefault("title", "系统通知");
        String content = (String) payload.get("content");
        
        if (content == null || content.trim().isEmpty()) {
            result.put("success", false);
            result.put("message", "通知内容不能为空");
            return ResponseEntity.badRequest().body(result);
        }
        
        // 移除标题和内容，剩余的作为附加参数
        Map<String, Object> params = new HashMap<>(payload);
        params.remove("title");
        params.remove("content");
        
        boolean success = notificationService.sendSystemNotificationToUser(userId, title, content, params);
        
        result.put("success", success);
        result.put("message", success ? "通知发送成功" : "通知发送失败，用户可能不在线");
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 发送系统警告给所有用户
     */
    @PostMapping("/warning/broadcast")
    public ResponseEntity<Map<String, Object>> broadcastWarning(@RequestBody Map<String, Object> payload) {
        Map<String, Object> result = new HashMap<>();
        
        String title = (String) payload.getOrDefault("title", "系统警告");
        String content = (String) payload.get("content");
        
        if (content == null || content.trim().isEmpty()) {
            result.put("success", false);
            result.put("message", "警告内容不能为空");
            return ResponseEntity.badRequest().body(result);
        }
        
        // 移除标题和内容，剩余的作为附加参数
        Map<String, Object> params = new HashMap<>(payload);
        params.remove("title");
        params.remove("content");
        
        int recipientCount = notificationService.sendSystemWarning(title, content, params);
        
        result.put("success", recipientCount > 0);
        result.put("recipientCount", recipientCount);
        result.put("message", recipientCount > 0 ? "警告发送成功" : "警告发送失败，没有在线用户");
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 发送系统警告给指定用户
     */
    @PostMapping("/warning/user/{userId}")
    public ResponseEntity<Map<String, Object>> sendWarningToUser(
            @PathVariable String userId,
            @RequestBody Map<String, Object> payload) {
        
        Map<String, Object> result = new HashMap<>();
        
        String title = (String) payload.getOrDefault("title", "系统警告");
        String content = (String) payload.get("content");
        
        if (content == null || content.trim().isEmpty()) {
            result.put("success", false);
            result.put("message", "警告内容不能为空");
            return ResponseEntity.badRequest().body(result);
        }
        
        // 移除标题和内容，剩余的作为附加参数
        Map<String, Object> params = new HashMap<>(payload);
        params.remove("title");
        params.remove("content");
        
        boolean success = notificationService.sendSystemWarningToUser(userId, title, content, params);
        
        result.put("success", success);
        result.put("message", success ? "警告发送成功" : "警告发送失败，用户可能不在线");
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 获取用户未读通知
     */
    @GetMapping("/user/{userId}/unread")
    public ResponseEntity<List<Message>> getUnreadNotifications(@PathVariable String userId) {
        List<Message> notifications = notificationService.getUnreadNotifications(userId);
        return ResponseEntity.ok(notifications);
    }
    
    /**
     * 标记通知为已读
     */
    @PostMapping("/user/{userId}/read/{messageId}")
    public ResponseEntity<Map<String, Object>> markAsRead(
            @PathVariable String userId,
            @PathVariable String messageId) {
        
        Map<String, Object> result = new HashMap<>();
        
        boolean success = notificationService.markNotificationAsRead(userId, messageId);
        
        result.put("success", success);
        result.put("message", success ? "已标记为已读" : "标记失败，可能通知不存在或已读");
        
        return ResponseEntity.ok(result);
    }
    
    /**
     * 标记所有通知为已读
     */
    @PostMapping("/user/{userId}/read-all")
    public ResponseEntity<Map<String, Object>> markAllAsRead(@PathVariable String userId) {
        Map<String, Object> result = new HashMap<>();
        
        int count = notificationService.markAllNotificationsAsRead(userId);
        
        result.put("success", true);
        result.put("count", count);
        result.put("message", count > 0 ? String.format("已将%d条通知标记为已读", count) : "没有未读通知");
        
        return ResponseEntity.ok(result);
    }
} 